Get Docker CE for CentOS
用户@主机名 ~% docker ps -a Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
此时已确定Docker本身已经安装正常。
问题原因是因为docker服务没有启动,所以在相应的/var/run/ 路径下找不到docker的进程。
执行
service docker start
命令,启动docker服务,返回
Redirecting to /bin/systemctl start docker.service
此时进程启动成功,再执行sudo docker ps -a,问题解决。
解决方案2:
安装docker hub,并运行
docker run --name repo alpine/git clone http://github.com/docker/getting-started.git docker cp repo:/git/getting-started/ . cd getting-started/ docker build -t docker101tutorial . docker run -d -p 80:80 --name docker-tutorial docker101tutorial docker tag docker101tutorial 793059909/docker101tutorial 在浏览器访问 localhost
一,Docker简介
百科说:Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
看起来有点雾,用过虚拟机的应该对虚拟化技术有点印象,不知道也没关系,就把它当成轻量级的虚拟机吧(虽然一个是完全虚拟化,一个是操作系统层虚拟化),这个解释到位:https://www.cnblogs.com/kex1n/p/6933039.html
百科又说:Docker 使用客户端-服务器 (C/S) 架构模式 使用远程API来管理和创建Docker容器。Docker 容器(Container)通过 Docker 镜像(Image)来创建,二者之间的关系类似于面向对象编程中的对象与类
那Docker由什么组成呢, 包括三个基本概念:
- 仓库(Repository)
- 镜像(Image)
- 容器(Container)
打个比方:你如果想玩英雄联盟中骚气的亚索,你首先得有这个英雄(Docker的镜像),然后你得花金币去英雄商店(Docker的仓库)买,接着进游戏就会看到一个半蹲的发型飘逸的剑客(Docker的容器),所以:
1,其中Registry是Docker用于存放镜像文件的仓库,Docker 仓库的概念跟Git 类似(就像商店存放所有的英雄,只是更改英雄的权限在某些非程序员手里)。
2,所谓镜像就是构建容器的源代码,是一个只读的模板,由一层一层的文件系统组成的,类似于虚拟机的镜像(英雄也是只读的,有自己的技能被动,你也不能进行操作)。
3,那么容器就是由Docker镜像创建的运行实例,类似于虚拟机,容器之间是相互隔离的,包含特定的应用及其所需的依赖文件(好比每个英雄都是隔离的,都有自己的皮肤,技能以及走的路线)。
注:Docker Hub
是Docker公司提供的一个注册服务器(Register)来保存多个仓库,每个仓库又可以包含多个具备不同tag的镜像
二,安装Docker
我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上, RHEL/Centos 的内核修补过, centos6.5的版本就可以——这个可以试试)
1,root账户登录,查看内核版本如下
[root@localhost ~]# uname -a Linux localhost.qgc 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2,(可选)把yum包更新到最新(生产环境慎重!yum update会对软件包和内核升级,此处只是为了排除系统环境的影响,来自笔者的备注—2019年10月30日)
[root@localhost ~]# yum update 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn 正在解决依赖关系 --> 正在检查事务 ---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7 将被 升级 ---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7_5.1 将被 更新 ---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7 将被 升级 ---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7_5.1 将被 更新 ---> 软件包 bind-license.noarch.32.9.9.4-61.el7 将被 升级 ---> 软件包 bind-license.noarch.32.9.9.4-61.el7_5.1 将被 更新
...
...
验证中 : 32:bind-license-9.9.4-61.el7.noarch 8/8
更新完毕:
bind-libs.x86_64 32:9.9.4-61.el7_5.1
bind-libs-lite.x86_64 32:9.9.4-61.el7_5.1
bind-license.noarch 32:9.9.4-61.el7_5.1
bind-utils.x86_64 32:9.9.4-61.el7_5.1
完毕!
[root@localhost ~]#
3,安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn ...
4,设置yum源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 已加载插件:fastestmirror, langpacks adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo
5,可以查看所有仓库中所有docker版本,并选择特定版本安装
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r 已加载插件:fastestmirror, langpacks 可安装的软件包 * updates: centos.ustc.edu.cn Loading mirror speeds from cached hostfile * extras: mirrors.aliyun.com docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable ...
6,安装Docker,命令:yum install docker-ce-版本号,我选的是17.12.1.ce,如下
[root@localhost ~]# yum install docker-ce-17.12.1.ce 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: centos.ustc.edu.cn base | 3.6 kB 00:00 docker-ce-stable | 2.9 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 正在解决依赖关系 --> 正在检查事务 ---> 软件包 docker-ce.x86_64.0.17.12.1.ce-1.el7.centos 将被 安装 --> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-17.12.1.ce-1.el7.centos.x86_64 需要
...
7, 启动Docker,命令:systemctl start docker,然后加入开机启动,如下
[root@localhost ~]# systemctl start docker [root@localhost ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
8,验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
[root@localhost ~]# docker version Client: Version: 17.12.1-ce API version: 1.35 Go version: go1.9.4 Git commit: 7390fc6 Built: Tue Feb 27 22:15:20 2018 OS/Arch: linux/amd64
Server: Engine: Version: 17.12.1-ce API version: 1.35 (minimum version 1.12) Go version: go1.9.4 Git commit: 7390fc6 Built: Tue Feb 27 22:17:54 2018 OS/Arch: linux/amd64 Experimental: false
备注:因为我已经安装过一次,部分贴图可能有出入,具体可参考博客:https://www.cnblogs.com/yufeng218/p/8370670.html
爬一些常用Docker命令,更多命令详解,请访问:http://www.docker.org.cn/dockerppt/106.html:
docker ps 查看当前正在运行的容器
docker ps -a 查看所有容器的状态
docker start/stop id/name 启动/停止某个容器
docker attach id 进入某个容器(使用exit退出后容器也跟着停止运行)
docker exec -ti id 启动一个伪终端以交互式的方式进入某个容器(使用exit退出后容器不停止运行)
docker images 查看本地镜像
docker rm id/name 删除某个容器
docker rmi id/name 删除某个镜像
docker run --name test -ti ubuntu /bin/bash 复制ubuntu容器并且重命名为test且运行,然后以伪终端交互式方式进入容器,运行bash
docker build -t soar/centos:7.1 . 通过当前目录下的Dockerfile创建一个名为soar/centos:7.1的镜像
docker run -d -p 2222:22 --name test soar/centos:7.1 以镜像soar/centos:7.1创建名为test的容器,并以后台模式运行,并做端口映射到宿主机2222端口,P参数重启容器宿主机端口会发生改变
To get started with Docker CE on CentOS, make sure you meet the prerequisites, then install Docker.
Prerequisites
Docker EE customers
To install Docker Enterprise Edition (Docker EE), go to Get Docker EE for CentOS instead of this topic.
To learn more about Docker EE, see Docker Enterprise Edition.
OS requirements
To install Docker CE, you need a maintained version of CentOS 7. Archived versions aren’t supported or tested.
The centos-extras
repository must be enabled. This repository is enabled by default, but if you have disabled it, you need to re-enable it.
The overlay2
storage driver is recommended.
Uninstall old versions
Older versions of Docker were called docker
or docker-engine
. If these are installed, uninstall them, along with associated dependencies.
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
It’s OK if yum
reports that none of these packages are installed.
The contents of /var/lib/docker/
, including images, containers, volumes, and networks, are preserved. The Docker CE package is now called docker-ce
.
Install Docker CE
You can install Docker CE in different ways, depending on your needs:
-
Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.
-
Some users download the RPM package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.
-
In testing and development environments, some users choose to use automated convenience scripts to install Docker.
Install using the repository
Before you install Docker CE for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
SET UP THE REPOSITORY
-
Install required packages.
yum-utils
provides theyum-config-manager
utility, anddevice-mapper-persistent-data
andlvm2
are required by thedevicemapper
storage driver.$ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
-
Use the following command to set up the stable repository. You always need the stable repository, even if you want to install builds from the edge or test repositories as well.
$ sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
-
Optional: Enable the edge and test repositories. These repositories are included in the
docker.repo
file above but are disabled by default. You can enable them alongside the stable repository.$ sudo yum-config-manager --enable docker-ce-edge
$ sudo yum-config-manager --enable docker-ce-test
You can disable the edge or test repository by running the
yum-config-manager
command with the--disable
flag. To re-enable it, use the--enable
flag. The following command disables the edge repository.$ sudo yum-config-manager --disable docker-ce-edge
Note: Starting with Docker 17.06, stable releases are also pushed to the edge and test repositories.
INSTALL DOCKER CE
-
Install the latest version of Docker CE, or go to the next step to install a specific version:
$ sudo yum install docker-ce
If prompted to accept the GPG key, verify that the fingerprint matches
060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
, and if so, accept it.Got multiple Docker repositories?
If you have multiple Docker repositories enabled, installing or updating without specifying a version in the
yum install
oryum update
command always installs the highest possible version, which may not be appropriate for your stability needs.Docker is installed but not started. The
docker
group is created, but no users are added to the group. -
To install a specific version of Docker CE, list the available versions in the repo, then select and install:
a. List and sort the versions available in your repo. This example sorts results by version number, highest to lowest, and is truncated:
$ yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
The list returned depends on which repositories are enabled, and is specific to your version of CentOS (indicated by the
.el7
suffix in this example).b. Install a specific version by its fully qualified package name, which is the package name (
docker-ce
) plus the version string (2nd column) up to the first hyphen, separated by a hyphen (-
), for example,docker-ce-18.03.0.ce
.$ sudo yum install docker-ce-<VERSION STRING>
Docker is installed but not started. The
docker
group is created, but no users are added to the group.[root@VM_0_15_centos ~]# yum list docker-ce --showduplicates | sort -r Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror, langpacks Installed Packages docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos @docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable Available Packages [root@VM_0_15_centos ~]# [root@VM_0_15_centos ~]# [root@VM_0_15_centos ~]# [root@VM_0_15_centos ~]# yum install docker-ce.x86_64-18.03.0.ce-1.el7.centos Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile No package docker-ce.x86_64-18.03.0.ce-1.el7.centos available. Error: Nothing to do [root@VM_0_15_centos ~]# yum install docker-ce-18.03.0.ce-1.el7.centos Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Package docker-ce-18.03.0.ce-1.el7.centos.x86_64 already installed and latest version Nothing to do [root@VM_0_15_centos ~]# [root@VM_0_15_centos ~]#
[root@VM_0_15_centos ~]# systemctl start docker [root@VM_0_15_centos ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world ca4f61b1923c: Pull complete Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
-
Start Docker.
$ sudo systemctl start docker
-
Verify that
docker
is installed correctly by running thehello-world
image.$ sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
Docker CE is installed and running. You need to use sudo
to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.
UPGRADE DOCKER CE
To upgrade Docker CE, follow the installation instructions, choosing the new version you want to install.
Install from a package
If you cannot use Docker’s repository to install Docker, you can download the .rpm
file for your release and install it manually. You need to download a new file each time you want to upgrade Docker.
-
Go to https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ and download the
.rpm
file for the Docker version you want to install.Note: To install an edge package, change the word
stable
in the above URL toedge
. Learn about stable and edgechannels. -
Install Docker CE, changing the path below to the path where you downloaded the Docker package.
$ sudo yum install /path/to/package.rpm
Docker is installed but not started. The
docker
group is created, but no users are added to the group. -
Start Docker.
$ sudo systemctl start docker
-
Verify that
docker
is installed correctly by running thehello-world
image.$ sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
Docker CE is installed and running. You need to use sudo
to run Docker commands. Continue to Post-installation steps for Linux to allow non-privileged users to run Docker commands and for other optional configuration steps.
UPGRADE DOCKER CE
To upgrade Docker CE, download the newer package file and repeat the installation procedure, using yum -y upgrade
instead of yum -y install
, and pointing to the new file.
Install using the convenience script
Docker provides convenience scripts at get.docker.com and test.docker.com for installing edge and testing versions of Docker CE into development environments quickly and non-interactively. The source code for the scripts is in the docker-install
repository. Using these scripts is not recommended for production environments, and you should understand the potential risks before you use them:
- The scripts require
root
orsudo
privileges to run. Therefore, you should carefully examine and audit the scripts before running them. - The scripts attempt to detect your Linux distribution and version and configure your package management system for you. In addition, the scripts do not allow you to customize any installation parameters. This may lead to an unsupported configuration, either from Docker’s point of view or from your own organization’s guidelines and standards.
- The scripts install all dependencies and recommendations of the package manager without asking for confirmation. This may install a large number of packages, depending on the current configuration of your host machine.
- The script does not provide options to specify which version of Docker to install, and installs the latest version that is released in the “edge” channel.
- Do not use the convenience script if Docker has already been installed on the host machine using another mechanism.
This example uses the script at get.docker.com to install the latest release of Docker CE on Linux. To install the latest testing version, use test.docker.com instead. In each of the commands below, replace each occurrence of get
with test
.
Warning:
Always examine scripts downloaded from the internet before running them locally.
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
<output truncated>
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker your-user
Remember to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group grants the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
Docker CE is installed. It starts automatically on DEB
-based distributions. On RPM
-based distributions, you need to start it manually using the appropriate systemctl
or service
command. As the message indicates, non-root users can’t run Docker commands by default.
UPGRADE DOCKER AFTER USING THE CONVENIENCE SCRIPT
If you installed Docker using the convenience script, you should upgrade Docker using your package manager directly. There is no advantage to re-running the convenience script, and it can cause issues if it attempts to re-add repositories which have already been added to the host machine.
Uninstall Docker CE
-
Uninstall the Docker package:
$ sudo yum remove docker-ce
-
Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
$ sudo rm -rf /var/lib/docker
You must delete any edited configuration files manually.
Next steps
-
Continue to Post-installation steps for Linux
-
Continue with the User Guide.
https://docs.docker.com/install/linux/docker-ce/centos/